using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ModWatch
{

	/// <summary>
	/// A <see cref="ModWatch.ModAlertExtenderBase"/> control that
	/// notifies the user of a change when the targeted <see cref="System.Web.UI.Control"/>
	/// is clicked.
	/// </summary>
	[TargetControlType(typeof(IButtonControl))]
	[TargetControlType(typeof(HyperLink))]
	public class OnClickModAlertExtender : ModAlertExtenderBase
	{

		/// <summary>
		/// Raises the <see cref="System.Web.UI.Control.Init"/> event.
		/// </summary>
		/// <param name="e">
		/// An <see cref="System.EventArgs"/>
		/// object that contains event data.
		/// </param>
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			this.GettingScriptReferences += new EventHandler<ScriptReferencesEventArgs>(this_GettingScriptReferences);
		}

		/// <summary>
		/// OnLoad override
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			// Register an empty OnSubmit statement so the ASP.NET WebForm_OnSubmit method will be automatically
			// created and our behavior will be able to wrap it if necessary
			ScriptManager.RegisterOnSubmitStatement(this, typeof(OnClickModAlertExtender), "OnClickModAlertExtenderOnSubmit", "null;");
		}

		/// <summary>
		/// Returns the <see cref="System.Web.UI.ScriptDescriptor"/>s required by
		/// this instance of <see cref="ModWatch.OnClickModAlertExtender"/>.
		/// </summary>
		/// <param name="targetControl">
		/// The <see cref="System.Web.UI.Control"/> being targeted.
		/// </param>
		/// <returns>
		/// A collection of <see cref="System.Web.UI.ScriptDescriptor"/>s
		/// defining this instance of <see cref="ModWatch.OnClickModAlertExtender"/>.
		/// </returns>
		protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors(Control targetControl)
		{
			string behaviorType = null;
			if (targetControl is IButtonControl)
				behaviorType = "ModWatch.OnButtonClickModAlertBehavior";
			else if (targetControl is HyperLink)
				behaviorType = "ModWatch.OnAnchorClickModAlertBehavior";
			else
				return new ScriptDescriptor[0];

			ScriptBehaviorDescriptor sd = new ScriptBehaviorDescriptor(behaviorType, targetControl.ClientID);
			AlertBehaviorBase alert = base.GetAlertBehaviorBase();
			if (alert != null)
				sd.AddComponentProperty("alertControl", alert.GetClientComponentID());
			sd.AddProperty("notifyModMessage", this.NotifyModMessage);
			sd.AddProperty("modGroup", this.ModGroup);

			return new ScriptDescriptor[] { sd };
		}

		/// <summary>
		/// Registers the scripts needed by the
		/// <see cref="ModWatch.OnClickModAlertExtender"/> class.
		/// </summary>
		/// <param name="sender">
		/// The <see cref="System.Object"/> that fired the
		/// <see cref="ModWatch.ModWatchExtenderBase.GettingScriptReferences"/> event.
		/// </param>
		/// <param name="e">
		/// A <see cref="ModWatch.ScriptReferencesEventArgs"/> object that contains the event data.
		/// </param>
		private void this_GettingScriptReferences(object sender, ScriptReferencesEventArgs e)
		{
			e.AddWebResourceScriptReference("ModWatch.Scripts.OnButtonClickModAlertBehavior.js");
			e.AddWebResourceScriptReference("ModWatch.Scripts.OnAnchorClickModAlertBehavior.js");
		}

	}

}
